package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserDao userDao;
    @PostMapping("/form")
    public String addUser(@RequestBody User user){
        userDao.addUser(user);
        return "添加成功";
    }

    @GetMapping("/users")
    public List<User> getallUsers(){
        List<User> users=userDao.getAllUser();
        return users;
    }

    @PutMapping("/users/{id}")
    public String updateUser(@PathVariable Integer id, @RequestBody User user){
        user.setId(id);
        userDao.updateUser(user);
        return "更新成功";
    }

    @DeleteMapping("/users/{id}")
    public String deleteUser(@PathVariable Integer id, @RequestParam(value = "hard", required = false, defaultValue = "false") boolean hard){
        if (hard) {
            userDao.deleteUserById(id);
            return "硬删除成功";
        } else {
            userDao.softDeleteUserById(id);
            return "软删除成功";
        }
    }


}
